<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/6/1
 * Time: 17:01
 */

namespace Admin\Controller;


use Common\Utils\PHPExcelUtils;
use Think\AjaxPage;
use Think\Upload;

class ShippingController extends BaseController
{
    static $UPLOAD_PATH = "Application/Public/Uploads/";
    /**
     * 快递公司列表
     */
    public function ajaxindex(){
        // 搜索条件
        $model = D('Shipping');

        //  搜索条件下 分页赋值
//        foreach($condition as $key=>$val) {
//            $Page->parameter[$key]   =   urlencode($val);
//        }
        $sort_order = I('order_by','shipping_id').' '.I('sort','desc');
        $shipping_name = I('shipping_name');
        if(!empty($shipping_name)){
            $map['shipping_name'] = $shipping_name;
        }
        $count = $model->where($map)->count();
        $Page  = new AjaxPage($count,10);
        $list = $model
            ->where($map)
            ->order($sort_order)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        $show = $Page->show();
        $this->assign('shippingList',$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    public function index(){
        $this->display();
    }

    public function save(){
        if(IS_POST){
            $model = D('Shipping');
            $data = $model->create(I('post.'), 1);
            if($data['shipping_id']){
                $res = D('Shipping')->where(array('shipping_id'=>$data['shipping_id']))->save($data);
            }else {
                $data['create_time'] = time();
                $res = D('Shipping')->add($data);
            }
            if($res){
                $this->success('保存成功',U('Shipping/index'));exit;
            }else{
                $this->error('保存失败',U('Shipping/index'));
            }
        }else{
            $id = I('shipping_id');
            if($id) {
                $Shipping = D('Shipping')->where(array('shipping_id'=>$id))->find();
                $this->assign('shipping', $Shipping);
            }
        }
        $this->display();
    }

    /**
     * 删除快递公司
     */
    public function delete(){
        $id = I('get.shipping_id');
        $row = M('Shipping')->where(array('shipping_id'=>$id))->delete();
        if($row){
            $this->success('成功删除快递');
        }else{
            $this->error('操作失败');
        }
    }
    private function uplad($field)
    {//文件上传部分
        $config = array(
            'rootPath' => self::$UPLOAD_PATH,
        );
        if (!is_dir(self::$UPLOAD_PATH)) {
            mkdir(self::$UPLOAD_PATH, 0755, true);
        }
        $upload = new Upload($config);
        if ($_FILES[$field]['error'] != 0) {
            $this->error($upload->getError());
            exit();
        }
        $info = $upload->uploadOne($_FILES[$field]);
//        $info   =   $upload->upload();
        return $info;
    }

    public function batchSave()
    {
        if(IS_POST) {

            $info = $this->uplad('file');
            if(!$info){
                $this->error('文件上传失败');
            }
            $resurl = self::$UPLOAD_PATH . $info['savepath'] . $info['savename'];  //$resurl 为excel的路径
//        $dataList[] = array();
            var_dump($resurl);
            $utils = new PHPExcelUtils();
            $model = D('Shipping');
            $res = $utils->read($resurl);

            foreach ($res as $k => $v) {
                if ($k == 1) {
                    continue;
                }  //如果是第一行则跳过
                $shipping_name = iconv('gb2312', 'UTF-8', $v[1]);//解决中文乱
                $data['shipping_code'] = $v[0];
                $data['shipping_name'] = $shipping_name;
//            var_dump($v);
//            $this->redirect("datalist", NULL, 25, $v[8]);
//            $data['description'] = $description;
                $dataList[] = $data;
//            if (!$result) {
//                $rerror = $rerror + 1;
//            }
            }
//        var_dump($dataList);
            try {
                $model->addAll($dataList);
            } catch (\Exception $e) {
                $this->error('批量导入失败,请检查数据格式');
                exit;
            }
            $this->success('批量导入成功');
            exit();
        }else{
            $this->display();
        }
    }

}